package top.yaofengqiao.springcloudsimple.gateway.util;

import com.alibaba.fastjson.JSON;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import top.yaofengqiao.springcloudsimple.common.util.R;

import java.nio.charset.StandardCharsets;

/**
 * @author yfq
 * @date 2024/5/27 13:21
 * @description 响应工具
 */
public class ResponseUtil {
    /**
     * 构建Mono返回结果
     */
    public static <T> Mono<Void> buildResponse(ServerHttpResponse response, R<T> r) {
        response.setStatusCode(HttpStatus.OK);
        byte[] bytes = JSON.toJSONString(r).getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = response.bufferFactory().wrap(bytes);
        return response.writeWith(Flux.just(buffer));
    }
}
